package com.honghu.villagepost.activity

import android.content.Context
import android.content.Intent
import android.os.Bundle
import androidx.recyclerview.widget.RecyclerView
import com.blankj.utilcode.util.ActivityUtils
import com.honghu.villagepost.R
import com.honghu.villagepost.adapter.OrderAdapter
import com.honghu.villagepost.api.ApiService
import com.honghu.villagepost.base.BaseListActivity
import com.honghu.villagepost.callback.DialogCallback
import com.honghu.villagepost.callback.LzyResponse
import com.honghu.villagepost.constant.Constants
import com.honghu.villagepost.entity.OrderDetails
import com.lzy.okgo.model.Response

/**
 * @author: mqwang
 * @create: 2/10/21 12:07 AM
 * @Description: 收货地址列表
 */
class OrderListActivity : BaseListActivity<OrderDetails>() {

    private var mType: Int = 0
    private val orderAdapter: OrderAdapter by lazy {
        OrderAdapter()
    }

    companion object {
        fun self(context: Context) {
            val intent = Intent(context, OrderListActivity::class.java)
            context.startActivity(intent)
        }
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        mType = intent.getIntExtra(Constants.TYPE, 0);
    }

    override fun bindRecyclerView(recLv: RecyclerView?) {
        recLv!!.adapter = orderAdapter
        setMainTitle("我的订单")

        orderAdapter.setOnItemClickListener { adapter, view, position ->
            var orderDetails = orderAdapter.getItem(position) as OrderDetails

        }
        orderAdapter.setOnItemChildClickListener { adapter, view, position ->
            var orderDetails = adapter.data.get(position) as OrderDetails
            when (view.id) {
                R.id.tv_status -> {
                    OrderDetailsActivity.self(this, orderDetails.orderNo)
                }
            }
        }
    }

    override fun onRightClickAction() {
        super.onRightClickAction()
        ActivityUtils.startActivity(AddAddrActivity::class.java)
    }

    override fun loadData(page: Int, pageCount: Int) {
        ApiService.order(this, mType, page, object : DialogCallback<LzyResponse<OrderDetails>>() {
            override fun onSuccess(response: Response<LzyResponse<OrderDetails>>?) {
                onGetDataSuc(response!!.body().data.records)
            }
        })
    }

    override fun getData(): MutableList<OrderDetails> {
        return orderAdapter.data
    }

    override fun bindData(isRefresh: Boolean, list: MutableList<OrderDetails>?) {
        when (isRefresh) {
            true -> {
                orderAdapter.setNewData(list)
            }
            false -> {
                orderAdapter.addData(list!!)
            }
        }
    }

}